<?php

namespace App\Controllers\City;

use App\Models\City\Zone;
use App\Libs\Helper\CommonHelper;

class GetServeZone extends \App\Libs\Base\BaseAction
{

    public function execute()
    {
        if (!$this->request->has('city')) {
            return \App\Libs\Views\Json::view(1, '缺少参数', null);
        }
        $city = $this->request->get('city', 'int', 0);
        $zoneModel = new Zone();
        $cities = $zoneModel->getCityArea($city);  //simple resultSet:如果没有就返回空
        if (empty($cities)) {
            return \App\Libs\Views\Json::view(1, '指定城市无数据', null);
        }
        $transResult = CommonHelper::setColToKey($cities, 'id');
        //去找这些区的下属区
        $areaId = array_keys($transResult);
        $childZone = $zoneModel->getZone($areaId);
        //合并一个区下的多个区域；
        $finalZone = [];
        foreach ($childZone as $key => $val) {
            $finalZone[$childZone[$key]['fid']][] = $val;
        }
	$result = [];
        foreach ($transResult as $key => $val) {
		$result[$key]['fdata'][] = $val;
            if (isset($finalZone[$transResult[$key]['id']])) {
                $result[$key]['child_zone'] = $finalZone[$transResult[$key]['id']];
            } else {
                $result[$key]['child_zone'] = [];
            }
        }
        return \App\Libs\Views\Json::view(0, '成功', array_values($result));
    }

}
